package app.base.systemmanage.systempermsmanage;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import app.base.common.Utility;

import app.base.systemmanage.systempermsmanage.dao.SysPermissionsDAO;
import app.base.systemmanage.systempermsmanage.model.SysPermissions;

public class SystemPermsLayerQuery extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
	    request.setCharacterEncoding(Utility.CHARSET_ENCODING_UTF8);
	    response.setContentType(Utility.DEFAULT_CONTENT_TYPE);

		PrintWriter out = response.getWriter(); 
		
		String node = URLDecoder.decode(request.getParameter("node"), Utility.CHARSET_ENCODING_UTF8);
		String leaf = URLDecoder.decode(request.getParameter("leaf"), Utility.CHARSET_ENCODING_UTF8);
		String module = request.getParameter("module");	
		
	    if (!(node == null || node.trim().length() == 0))
	    {
		    node = URLDecoder.decode(node, Utility.CHARSET_ENCODING_UTF8).trim();
		    
			if (!(module == null))
			{
				module = URLDecoder.decode(module, Utility.CHARSET_ENCODING_UTF8).trim();
				node = node.substring(0, node.length() - module.length());
			}
	    	
			if (leaf == null || leaf.trim().length() == 0)
			{
				leaf = "2";
			}
			
		    out.println(getTreeSubNodesByNodeId(node, leaf, module).toString());
	    }
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
		doGet(request, response);
	}
	
	//return JSON format.
	private StringBuffer getTreeSubNodesByNodeId(String nodeId, String leaf, String module)
	{
		StringBuffer json = new StringBuffer("[");
		
		ApplicationContext context = new ClassPathXmlApplicationContext(Utility.DEFAULT_SPRINGFRAMEWORK_APPLICATION_CONTEXT_FILE_PATH);
		SysPermissionsDAO sysPermissionsDAO = (SysPermissionsDAO)context.getBean(Utility.SYSTEM_PERMISSIONS_MANAGE_MODULE_BEAN_NAME);
		
		Map<String, Object>  map = new HashMap<String, Object>();
		
		map.put("PERM_XTYPE_PARENT_ID", nodeId);
		map.put("LEAF_NODE_FLAG", leaf);
		
		List<SysPermissions> list = sysPermissionsDAO.select_0001(map);
			
		for(int index = 0; index < list.size(); index++)
		{
			SysPermissions sysPermissions = (SysPermissions) list.get(index);
			
			String permId = (module == null) ? sysPermissions.getPERM_XTYPE_ID().trim() : sysPermissions.getPERM_XTYPE_ID().trim() + module;
			String permParentId = sysPermissions.getPERM_XTYPE_PARENT_ID().trim();
			String permName = sysPermissions.getPERM_XTYPE_NAME().trim();
			String permDesc = (sysPermissions.getPERM_XTYPE_DESC() == null) ? "" : sysPermissions.getPERM_XTYPE_DESC().trim();
			String permCode = (sysPermissions.getPERM_CODE() == null) ? "" : sysPermissions.getPERM_CODE().trim();
			String normalIcon = sysPermissions.getNORMAL_IMG_URL().trim();
			String leafNodeFlag = sysPermissions.getLEAF_NODE_FLAG().trim();
			String expandIcon = (sysPermissions.getEXPAND_IMG_URL() == null) ? "" : sysPermissions.getEXPAND_IMG_URL().trim();
			String displayRankingIndex = sysPermissions.getDISPLAY_RANKING_INDEX().toString().trim();
			String sysHoldFlag = sysPermissions.getSYS_HOLD_FLAG().trim();
			String state = sysPermissions.getSTATE().trim();

			json.append("{id: '" + permId + "',");
			json.append("roleParentId: '" + permParentId + "',");
			json.append("text: '" + permName + "',");
			json.append("qtip: '" + permDesc + "',");
			json.append("permCode: '" + permCode + "',");
			json.append(leafNodeFlag.equals("1") ? "leaf: true," : "leaf: false,");
			json.append("icon: '" + normalIcon + "',");
			json.append("expandIcon: '" + expandIcon + "',");
			json.append("displayRankingIndex: '" + displayRankingIndex + "',");
			json.append("sysHoldFlag: '" + sysHoldFlag + "',");
			json.append("state: '" + state + "',");
			json.append("singleClickExpand: true");

			json.append("}");
			
			if (index < list.size() - 1)
			{
				json.append(", ");
			}
		}
		
		json.append("]");
		
		return json;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}